package com.xinxing.city.recoverycheck.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xinxing.city.recoverycheck.DI;
import com.xinxing.city.recoverycheck.R;
import com.xinxing.city.recoverycheck.databinding.ActivityDoorBinding;
import com.xinxing.city.recoverycheck.entitys.DeviceConfig;
import com.xinxing.city.recoverycheck.entitys.KernelAction;
import com.xinxing.city.recoverycheck.entitys.KernelActions;
import com.xinxing.city.recoverycheck.entitys.WeightUp;
import com.xinxing.city.recoverycheck.interfaces.IDeviceKernel;
import com.xinxing.city.recoverycheck.util.StringUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class DoorActivity extends AppCompatActivity {
    private ActivityDoorBinding binding;
    private IDeviceKernel deviceKernel;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_door);
        deviceKernel = DI.getInstance(IDeviceKernel.class);
        EventBus.getDefault().register(this);
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DoorActivity.this.finish();
            }
        });
        binding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String et = binding.et.getText().toString();
                if(!et.equals("")&& StringUtil.isNumeric(et)){
                    int k = Integer.valueOf(et);
                    if(k<=240){
                        DeviceConfig deviceConfig = new DeviceConfig();
                        deviceConfig.setDoorid(1);
                        deviceConfig.setDoorOpen(1);
                        deviceConfig.setDoorClose(1);
                        deviceConfig.setDistanceDn(30);
                        deviceConfig.setDistanceUp(20);
                        deviceConfig.setAutoCloseTime(k);
                        deviceConfig.setNowStatus(60);
                        List<DeviceConfig> deviceConfigs = new ArrayList<>();
                        deviceConfigs.add(deviceConfig);
                        Gson g = new Gson();
                        String str = g.toJson(deviceConfigs);
                        Log.e("TT",StringUtil.stringToHex(str));
                       boolean b= deviceKernel.getSerial().sendConfig(StringUtil.stringToHex(str));
                       if(b){
                           Toast.makeText(DoorActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                       }else{
                           Toast.makeText(DoorActivity.this,"保存失败，请稍后重试",Toast.LENGTH_SHORT).show();
                       }
                    }else{
                        Toast.makeText(DoorActivity.this,"自动关门时间不能超过240",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        binding.test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deviceKernel.getSerial().getWeight(1);
//                deviceKernel.getSerial().openDoor(1);
            }
        });

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onKernelEvent(KernelAction kernelAction) {
        int actionType = kernelAction.getActionType();
        List<String> params = kernelAction.getActionParams();
        switch (actionType) {
            case KernelActions.R_16_HEART:
                if (params.size() > 0) {
                    String jsn = params.get(0);
                    Log.e("TTT", "R_16_HEART->>>   " + params.get(0));
                    Gson gson = new Gson();
                    Type dev = new TypeToken<List<WeightUp>>() {
                    }.getType();

                    List<WeightUp> weightUps = gson.fromJson(jsn, dev);
                    for (WeightUp weight2 : weightUps) {
                        if (weight2.getDoorid() == 1) {
                            binding.time.setText("设置时间为："+weight2.getAutoCloseTime());
                        }

                    }

                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
